package com.example.book.model;

import com.example.book.constant.Constants;
import lombok.Data;
import org.yaml.snakeyaml.scanner.Constant;

@Data
public class Result<T> {
    //1.用户登录，程序没出错200
    //2.用户未登录-1
    //3.用户登录，程序出错500
    //所以可以加一个状态码
    private int code;//200-成功  -1-失败  -2-程序出错  业务状态码，非http状态码
    private String errMsg;
    private T data;


    //如果不是static方法可以写成public Result<T> success...
    public static <T> Result success(T data) {
        Result result = new Result();
        result.setCode(Constants.SUCCESS_CODE);
        result.setErrMsg("");
        result.setData(data);
        return result;
    }

    public static <T> Result unlogin() {
        Result result = new Result();
        result.setCode(Constants.UNFOGIN_CODE);
        result.setErrMsg("");
        return result;
    }

    public static <T> Result fail(T data) {
        Result result = new Result();
        result.setCode(Constants.FATL_CODE);
        result.setErrMsg("程序发生错误！");
        return result;
    }
    public static <T> Result fail(String errMsg) {
        Result result = new Result();
        result.setCode(Constants.FATL_CODE);
        result.setErrMsg(errMsg);
        return result;
    }
    public static <T> Result fail(String errMsg,int code) {
        Result result = new Result();
        result.setCode(code);
        result.setErrMsg(errMsg);
        return result;
    }
    public static <T> Result fail() {
        Result result = new Result();
        result.setCode(Constants.FATL_CODE);
        result.setErrMsg("程序发生错误");
        return result;
    }
}
